/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.wsgi.provider;

import static cc.aileron.commons.utility.ResourceUtils.resource;
import static cc.aileron.wsgi.navigation.WsgiNavigation.State.End;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import cc.aileron.commons.accessor.PojoAccessorFactory;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.template.exception.CompileException;
import cc.aileron.template.xml.XmlTemplate;
import cc.aileron.template.xml.XmlTemplateCompiler;
import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.context.WsgiProcessContext;
import cc.aileron.wsgi.navigation.WsgiNavigation;

import com.google.inject.Inject;

/**
 * WsgiTemplateProvider 開発用
 * 
 * @author Aileron
 * 
 */
public class WsgiTemplateProviderSuccessiveCompile implements
    WsgiTemplateProvider
{
    @Override
    public <T> WsgiProcess<T> get(final String path, final String contentType) throws CompileException,
            ResourceNotFoundException
    {
        return new WsgiProcess<T>()
        {
            public WsgiNavigation doProcess(final T dto) throws Exception
            {
                final HttpServletResponse response = context.getResponse();
                response.setContentType(contentType);
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);

                final XmlTemplate template = compiler.compile(resource(path)
                        .toFileContent());
                template.parse(response.getWriter(), factory.accessor(dto));

                return End.navigation();
            }
        };
    }

    @Override
    public <T> WsgiProcess<T> get(final String path,
            final String contentType,
            final String encode) throws CompileException,
            ResourceNotFoundException
    {
        return new WsgiProcess<T>()
        {
            public WsgiNavigation doProcess(final T dto) throws Exception
            {
                final HttpServletResponse response = context.getResponse();
                response.setContentType(contentType);
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);

                final PrintWriter out = new PrintWriter(new OutputStreamWriter(
                        response.getOutputStream(), encode));

                final XmlTemplate template = compiler.compile(resource(path)
                        .toFileContent());
                template.parse(out, factory.accessor(dto));

                return End.navigation();
            }
        };
    }

    /**
     * constractor injection
     * 
     * @param compiler
     * @param factory
     * @param context
     */
    @Inject
    public WsgiTemplateProviderSuccessiveCompile(
        final XmlTemplateCompiler compiler,
        final PojoAccessorFactory factory,
        final WsgiProcessContext context)
    {
        this.context = context;
        this.compiler = compiler;
        this.factory = factory;
    }

    /**
     * compiler
     */
    final XmlTemplateCompiler compiler;

    /**
     * factory
     */
    final PojoAccessorFactory factory;

    /**
     * context
     */
    final WsgiProcessContext context;
}